import { createWebHashHistory, createRouter } from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Dashboard from '../components/Dashboard.vue'
import AccountList from '../components/AccountList.vue'
import ProductList from '../components/ProductList.vue'
import AddAccount from '../components/AddAccount.vue'
import Profile from '../components/Profile.vue'
import ChangePassword from '../components/ChangePassword.vue'
import ProductAdd from '../components/ProductAdd.vue'
import CommodityClassification from '../components/CommodityClassification.vue'
import OrderManagement from '../components/OrderManagement.vue'
import Store from '../components/Store.vue'
import CommodityStatistics from '../components/CommodityStatistics.vue'
import OrderStatistics from '../components/OrderStatistics.vue'
import Rights from '../components/Rights.vue'
import Role from '../components/Role.vue'

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: {
          title: '首页'
        }
      },
      {
        path: '/account-list',
        name: 'AccountList',
        component: AccountList,
        meta: {
          title: '账号列表',
          parent: '账号管理'
        }
      },
      {
        path: '/add-account',
        name: 'AddAccount',
        component: AddAccount,
        meta: {
          title: '添加账号',
          parent: '账号管理'
        }
      },
      {
        path: '/profile',
        name: 'Profile',
        component: Profile,
        meta: {
          title: '个人中心',
          parent: '账号管理'
        }
      },
      {
        path: '/change-password',
        name: 'ChangePassword',
        component: ChangePassword,
        meta: {
          title: '修改密码',
          parent: '账号管理'
        }
      },
      {
        path: '/productlist',
        name: 'ProductList',
        component: ProductList,
        meta: {
          title: '商品列表',
          parent: '商品管理'
        }
      },
      {
        path: '/productadd',
        name: 'ProductAdd',
        component: ProductAdd,
        meta: {
          title: '商品添加',
          parent: '商品管理'
        }
      },
      {
        path: '/commodityclassification',
        name: 'CommodityClassification',
        component: CommodityClassification,
        meta: {
          title: '商品分类',
          parent: '商品管理'
        }
      },
      {
        path: '/ordermanagement',
        name: 'OrderManagement',
        component: OrderManagement,
        meta: {
          title: '订单管理'
        }
      },
      {
        path: '/store',
        name: 'Store',
        component: Store,
        meta: {
          title: '店铺管理'
        }
      },
      {
        path: '/commoditystatistics',
        name: 'CommodityStatistics',
        component: CommodityStatistics,
        meta: {
          title: '商品统计',
          parent: '数据统计'
        }
      },
      {
        path: '/orderstatistics',
        name: 'OrderStatistics',
        component: OrderStatistics,
        meta: {
          title: '订单统计',
          parent: '数据统计'
        }
      },
      {
        path: '/rights',
        name: 'Rights',
        component: Rights,
        meta: {
          title: '权限管理',
          parent: '权限管理'
        }
      },
      {
        path: '/role',
        name: 'Role',
        component: Role,
        meta: {
          title: '角色管理',
          parent: '权限管理'
        }
      }
    ]
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('../404/Error.vue'),
    meta: { title: '404' }
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }

]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to) => {
  const publicPaths = ['/', '/register', '/404', '/resetpassword'];
  
  // 直接放行白名单路径
  if (publicPaths.includes(to.path)) return true;
  
  // 处理404路径的特殊情况
  if (to.name === 'NotFound') return true;
  
  // 登录验证
  if (!sessionStorage.getItem('user')) {
    return { path: '/' }; // 重定向到登录页
  }
  
  return true;
});

export default router